/*
 * @Date: 2023-03-15 12:05:11
 * @LastEditTime: 2023-03-28 15:38:39
 * @FilePath: /NLK/src/sdk/index.ts
 * 介绍:
 */
/**格式化失败返回数据 */
import BN from "bn.js";
import { pow, divide } from "mathjs";

/**发起支付金额精度调整 */
export function parseAmount(
  amount: number | string | void | null,
  format: number
): string {
  if (!amount) return "0";
  const cause = Math.pow(10, 8);  
  amount = Math.floor(Number(amount) * cause); 
  const formatNumber = new BN(10).pow(new BN(format));  
  return new BN(amount).mul(formatNumber).div(new BN(cause)).toString();
}

/**查询余额金额精度调整 */
export function parseBalance(
  balance: number | string | void | null,
  format: number
) {
  const formatNumber = pow(10, format);
  return divide(Number(balance), formatNumber);
}
export function sdkFail<T>(message: string, data?: any): SdkRes<T> {
  console.error("sdk失败:" + message, data);
  return {
    message: message || data,
    status: false,
    info: data,
  };
}
/**格式化成功返回数据 */
export function sdkSucceed<T>(message: string, data?: any): SdkRes<T> {
  console.info("sdk成功:", message, data);
  return {
    message,
    status: true,
    data,
  };
}

export interface SdkRes<T> {
  message: string;
  /**获取成功失败状态 */
  status: boolean;
  data?: T;
  info?: T;
}
